package com.bangcommunity.bbframe.mlog.worker;

import com.bangcommunity.bbframe.common.utils.thread.NamedThreadFactory;
import com.bangcommunity.bbframe.mlog.collector.StatisticCollector;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * @author tanghc
 * @date 18/1/31
 */
public class StatisticSchedule {
    static Logger logger = LoggerFactory.getLogger(StatisticSchedule.class);
    /** 定时任务执行器 */
    private static final ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(3,
            new NamedThreadFactory("MLogStat", true));

    public static void start() {
        logger.debug("start StatisticSchedule");
        scheduledExecutorService.scheduleWithFixedDelay(() -> {
            // 收集统计信息
            try {
                StatisticCollector.report();
            } catch (Throwable t) { // 防御性容错
                logger.error("Unexpected error occur at send StatisticSchedule statistic, cause: " + t.getMessage(), t);
            }
        }, 10, 10, TimeUnit.SECONDS);

    }
}
